/*
 * Copyright (C) 2005, Velocityme Partnership
 * TaskDependencyBean.java
 *
 * Created on 6 August 2005, 05:00
 */

package com.velocityme.entity;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import java.util.*;
import com.velocityme.interfaces.*;
import com.velocityme.interfaces.ServiceUnavailableException;

/**
 *
 * @author  Robert
 * @ejb.bean
 *           type="CMP"
 *           cmp-version="2.x"
 *           name="TaskDependency"
 *           jndi-name="ejb/TaskDependency"
 *           view-type="local"
 * @ejb.transaction type="Required"
 *
 * @ejb.value-object match="*"
 *
 * @ejb.persistence table-name="taskdependency"
 *
 * @ejb.util generate="physical"
 *
 * @ejb.finder signature="com.velocityme.interfaces.TaskDependency findByTasks(com.velocityme.interfaces.TaskLocal dependencyTask, com.velocityme.interfaces.TaskLocal dependantTask)"
 *              query="SELECT OBJECT(o) FROM TaskDependency o WHERE o.dependencyTaskLocal = ?1 AND o.dependantTaskLocal = ?2"
 *
 * @jboss.persistence create-table="true"
 *                    remove-table="false"
 */
public abstract class TaskDependencyBean implements EntityBean {
    
    /**
    * Context set by container
    */
    private javax.ejb.EntityContext m_entityContext;

    public void setEntityContext(javax.ejb.EntityContext entityContext) {
        m_entityContext = entityContext;
    }

    public void unsetEntityContext() {
        m_entityContext = null;
    }
  
    /** @ejb.create-method */
    public com.velocityme.interfaces.TaskDependencyPK ejbCreate(com.velocityme.interfaces.TaskLocal p_dependencyTaskLocal,
                                                                com.velocityme.interfaces.TaskLocal p_dependantTaskLocal) throws CreateException {
        // make sure that this link does not already exist
        try {
            TaskDependencyUtil.getLocalHome().findByTasks(p_dependencyTaskLocal, p_dependantTaskLocal);
            throw new CreateException();
        } catch(FinderException e) {
            // it doesn't exist so we can continue
        } catch(NamingException e) {
            throw new CreateException(e.getMessage());
        }
        try {
            setTaskDependencyId(generateNewID());
        }
        catch(ServiceUnavailableException e) {
            throw new EJBException(e.getMessage());
        }
        // EJB 2.0 spec says return null for CMP ejbCreate methods.
        return null;
    }

    public void ejbPostCreate(com.velocityme.interfaces.TaskLocal p_dependencyTaskLocal,
                              com.velocityme.interfaces.TaskLocal p_dependantTaskLocal) throws CreateException {
        // CMR relations must be set in ejbPostCreate
        setDependencyTaskLocal(p_dependencyTaskLocal);
        setDependantTaskLocal(p_dependantTaskLocal);
    }

    /**
     * Retrive a unique creation id to use for this bean.  This will end up
     * demarcating this bean from others when it is stored as a record
     * in the database.
     *
     * @return Returns an integer that can be used as a unique creation id.
     *
     * @throws ServiceUnavailableException Indicating that it was not possible
     *                                     to retrieve a new unqiue ID because
     *                                     the service is not available
     **/
    private Integer generateNewID() throws ServiceUnavailableException {
        int lUniqueId = -1;
        try {            
            SequenceGenerator lBean = SequenceGeneratorUtil.getHome().create();
            lUniqueId = lBean.getNextNumber("taskdependency","taskDependencyId");
            lBean.remove();
        }
        catch ( NamingException ne ) {
            throw new ServiceUnavailableException( "Naming lookup failure: " + ne.getMessage() );
        }
        catch ( CreateException ce ) {
            throw new ServiceUnavailableException( "Failure while creating a generator session bean: " + ce.getMessage() );
        }
        catch ( RemoveException re ) {
            // When the Bean cannot be removed after a while it will be taken back by the container
            // therefore ignore this exception
        }
        catch ( RemoteException rte ) {
            throw new ServiceUnavailableException( "Remote exception occured while accessing generator session bean: " +  rte.getMessage() );
        }
        
        return new Integer(lUniqueId);
    }
    
    /** @ejb.interface-method view-type="local" */
    public abstract com.velocityme.valueobjects.TaskDependencyValue getTaskDependencyValue();

    //=======================================================================
    //
    //                   ATTRIBUTES
    //
    //=======================================================================
    
    /**
     * @ejb.persistence column-name="taskDependencyId"
     * @ejb.interface-method view-type="local"
     * @ejb.pk-field 
     */
    public abstract java.lang.Integer getTaskDependencyId();
    public abstract void setTaskDependencyId(java.lang.Integer taskDependencyId);
    
    //=======================================================================
    //
    //                   RELATIONSHIPS
    //
    //=======================================================================
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="TaskDependency-DependencyTask"
     *               role-name="TaskDependency-has-a-dependency-Task"
     * @jboss.relation fk-column="dependencyTaskIdFk"
     *                 related-pk-field="taskId"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.TaskValue"
     *                   aggregate-name="DependencyTaskValue"
     */
    public abstract com.velocityme.interfaces.TaskLocal getDependencyTaskLocal();
    /** @ejb.interface-method view-type="local"  */
    public abstract void setDependencyTaskLocal(com.velocityme.interfaces.TaskLocal dependencyTask);
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="TaskDependency-DependantTask"
     *               role-name="TaskDependency-has-a-dependant-Task"
     * @jboss.relation fk-column="dependantTaskIdFk"
     *                 related-pk-field="taskId"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.TaskValue"
     *                   aggregate-name="DependantTaskValue"
     */
    public abstract com.velocityme.interfaces.TaskLocal getDependantTaskLocal();
    /** @ejb.interface-method view-type="local"  */
    public abstract void setDependantTaskLocal(com.velocityme.interfaces.TaskLocal dependantTask);
    
}
